Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add from_bytes_dict alternative constructor for HttpResponseHeaders #33

Merged
merged 3 commits into from
Apr 11, 2022

Conversation

BurnzZ
Copy link
Contributor

@BurnzZ BurnzZ commented Mar 28, 2022

From the recent enhancement in #30, it would seem that we'll need to support insantiating the HttpResponseHeaders from raw bytes values. In particular, frameworks like Scrapy uses header values in bytes format.

By default, https://github.com/aio-libs/multidict only supports strings and not bytes. Thus, an alternative constructor is proposed for ease of use.

@BurnzZ BurnzZ requested a review from kmike March 28, 2022 11:14
@codecov
Copy link

codecov bot commented Mar 28, 2022

Codecov Report

Merging #33 (5caef5f) into master (256a0c3) will not change coverage.
The diff coverage is 100.00%.

❗ Current head 5caef5f differs from pull request most recent head ddb7d20. Consider uploading reports for the commit ddb7d20 to get more accurate results

@@            Coverage Diff            @@
##            master       #33   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         6    +1     
  Lines          127       224   +97     
=========================================
+ Hits           127       224   +97     
Impacted Files Coverage Δ
web_poet/__init__.py 100.00% <100.00%> (ø)
web_poet/overrides.py 100.00% <100.00%> (ø)
web_poet/page_inputs.py 100.00% <100.00%> (ø)
web_poet/utils.py 100.00% <100.00%> (ø)

alongside a plain ``bytes`` value.

By default, it converts the ``bytes`` value using "utf-8". However, this
can easily be overridden using the ``encoding`` parameter.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This utf-8 default value was based on how Scrapy was using it as the default for its Headers.

@@ -74,6 +75,45 @@ def from_name_value_pairs(cls: Type[T_headers], arg: List[Dict]) -> T_headers:
"""
return cls([(pair["name"], pair["value"]) for pair in arg])

@classmethod
def from_bytes(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name may be a bit misleading, one may think this creates Headers from HTTP response bytes which represent the headers. Maybe something like from_bytes_dict? Or maybe even more specific, from_scrapy_headers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! Renaming it to from_scrapy_headers seems to be too specific. Other non-scrapy libs could have the same setup. Having it as from_bytes_dict makes it more clear and semantically more meaningful at the same time. Updated this on eb18427

if isinstance(data, str):
return data
elif isinstance(data, bytes):
return data.decode(encoding)
Copy link
Member

@kmike kmike Apr 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should raise an exception if data is not bytes or str

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Adressed this in ddb7d20 alongside the ability to handle Tuple alongside List.

@BurnzZ BurnzZ changed the title add from_bytes alternative constructor for HttpResponseHeaders add from_bytes_dict alternative constructor for HttpResponseHeaders Apr 11, 2022
@BurnzZ BurnzZ requested a review from kmike April 11, 2022 04:58
@kmike kmike merged commit a29d86d into master Apr 11, 2022
@kmike
Copy link
Member

kmike commented Apr 11, 2022

Thanks @BurnzZ!

@BurnzZ BurnzZ deleted the from_bytes branch April 11, 2022 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants